package com.ycsj.goldmedalnewconcept.common.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.adapter.VersionRvAdapter;
import com.ycsj.goldmedalnewconcept.common.bean.VersionInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 通用的版本介绍页面
 */
public class VerionInfoActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoadingView;

    //最新版本
    @BindView(R.id.tv_version)
    TextView tvVersion;

    //空视图
    private View emptyView;

    //数据集合
    private List<VersionInfo> oldDatas;

    //适配器
    private VersionRvAdapter adapter;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_version_info;
    }

    @Override
    protected void init() {

        //沉浸状态栏
        initStateBar();

        //初始化标题栏
        initTitle();


        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("版本介绍");
    }


    /**
     * 设置列表
     */
    private void initRv() {

        oldDatas = new ArrayList<>();
//        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
//        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
//        tvMessage.setText("暂无版本介绍");
        adapter = new VersionRvAdapter(this, R.layout.list_newversion, oldDatas);
        BqaManager.setRv(null, this, adapter, rv);

    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({ R.id.iv_back})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

    /**
     * 显示应用的版本号
     */
    private static String getVersionName() {
        PackageManager packageManager = BaseApplication.context.getPackageManager();
        String versionName = null;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    BaseApplication.context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("apptype", "Android");
        map.put("page", "0");

        HttpUtils.post(HttpAddress.VERSIONINFO, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoadingView, null, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    JSONObject object = null;
                    try {
                        object = new JSONObject(response);

                        // 获取返回的array数组
                        JSONArray array = object.getJSONArray("list");
                        final List<VersionInfo> newDatas = new ArrayList<>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONArray res = (JSONArray) array.get(i);
                            VersionInfo info = new VersionInfo();
                            info.setName(res.getString(1));
                            info.setContent(res.getString(4));
                            info.setVersion(res.getString(3));
                            newDatas.add(info);
                        }
                        oldDatas.clear();
                        oldDatas.addAll(newDatas);
                        BqaManager.setRv(emptyView, VerionInfoActivity.this, adapter, rv, null);
                        rlLoadingView.setVisibility(View.GONE);

                        if(oldDatas!=null&&oldDatas.size()!=0&&oldDatas.get(0)!=null){
//                            tvVersion.setText("金牌君("+oldDatas.get(0).getVersion()+")");
                            tvVersion.setText("金牌君("+getVersionName()+")");

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }
}
